package umich.max.geolocation.annotation;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import max.nlp.dal.blog.blogger.BloggerBlog;
import max.nlp.dal.blog.blogger.BloggerDB;
import max.nlp.dal.blog.blogger.BloggerPost;
import max.nlp.wrappers.stanford.StanfordNLP;

import org.apache.commons.lang.StringEscapeUtils;

import com.mongodb.DBCursor;

public class ExtractBlogsToCSV {

	public static void main(String[] args) {
		extraBlogsAndSentences(100, "/home/max/blogs.txt", "/home/max/sentences.txt");
	}

	public static void extraBlogsAndSentences(int numBlogs, String blogFile,
			String sentenceFile) {
		try {
			PrintWriter blogWriter = new PrintWriter(new FileWriter(new File(
					blogFile)));
			PrintWriter sentenceWriter = new PrintWriter(new FileWriter(
					new File(sentenceFile)));

			BloggerDB db = BloggerDB.getInstance();
			DBCursor itr = db.getBlogIterator();
			int counter = 0;
			while (itr.hasNext() && (counter <= numBlogs)) {
				BloggerBlog b = new BloggerBlog(itr.next());
				extractBlog(b, blogWriter);
				extractSentences(b, sentenceWriter);
				if (counter % 100 == 0){
					blogWriter.flush();
					sentenceWriter.flush();
				}
				counter++;
			}
			blogWriter.flush();
			sentenceWriter.flush();
			blogWriter.close();
			sentenceWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Prints blog posts to a csv file
	 * 
	 * @param numBlogs
	 *            max number blogs
	 * @param file
	 *            output file.
	 */
	public static void extractBlog(BloggerBlog b, PrintWriter w) {
		List<BloggerPost> posts = b.getPosts();
		for (BloggerPost post : posts) {
			w.println(post.getOriginalPost().getId() + ","
					+ StringEscapeUtils.escapeCsv(post.getCleanContent()));
		}
	}


	public static void extractSentences(BloggerBlog b,
			PrintWriter sentenceWriter) {

		StanfordNLP stanford = StanfordNLP.getInstance();
		List<BloggerPost> posts = b.getPosts();
		for (BloggerPost post : posts) {
			String text = post.getCleanContent();
			List<String> sentences = stanford.extractSentences("ssplit", text);
			for (String sentence : sentences) {
				sentenceWriter.println(post.getOriginalPost().getId() + "],"
						+ StringEscapeUtils.escapeCsv(sentence));
			}
			
		}

	}
}
